package com.yic.module.mes.service.itemproduce;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.yic.module.mes.controller.admin.itemproduceroute.vo.ItemProduceRouteCreateReqVO;
import com.yic.module.mes.controller.admin.itemproduceroute.vo.ItemProduceRouteRespVO;
import com.yic.module.mes.service.itemproduceroute.ItemProduceRouteService;
import com.yic.module.mes.service.plan.PlanService;
import com.yic.module.system.enums.dict.SystemBooleanEnum;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.yic.module.mes.controller.admin.itemproduce.vo.*;
import com.yic.module.mes.dal.dataobject.itemproduce.ItemProduceDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.mes.convert.itemproduce.ItemProduceConvert;
import com.yic.module.mes.dal.mysql.itemproduce.ItemProduceMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.mes.enums.ErrorCodeConstants.*;

/**
 * 产品生产信息 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class ItemProduceServiceImpl implements ItemProduceService {

    @Resource
    private ItemProduceMapper itemProduceMapper;
    @Resource
    private ItemProduceRouteService itemProduceRouteService;
    @Resource
    private PlanService planService;
    @Resource
    private TransactionTemplate transactionTemplate;

    @Override
    public Long createItemProduce(ItemProduceCreateReqVO createReqVO) {
        validateCreateOrUpdate(null, createReqVO.getItemId(), createReqVO.getFactoryId());
        // 插入
        ItemProduceDO itemProduce = ItemProduceConvert.INSTANCE.convert(createReqVO);
        transactionTemplate.execute(status -> {
            itemProduceMapper.insert(itemProduce);
            itemProduceRouteService.createItemProduceRoute(createReqVO.getRouteList(), itemProduce.getId());
            return Boolean.TRUE;
        });
        // 返回
        return itemProduce.getId();
    }

    @Override
    public void updateItemProduce(ItemProduceUpdateReqVO updateReqVO) {
        // 校验存在
        validateCreateOrUpdate(updateReqVO.getId(), updateReqVO.getItemId(), updateReqVO.getFactoryId());
        // 更新
        ItemProduceDO updateObj = ItemProduceConvert.INSTANCE.convert(updateReqVO);

        transactionTemplate.execute(status -> {
            itemProduceMapper.updateById(updateObj);
            itemProduceRouteService.createItemProduceRoute(updateReqVO.getRouteList(), updateObj.getId());
            return Boolean.TRUE;
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteItemProduce(Long id) {
        // 校验存在
        validateItemProduceExists(id);
        // 校验产品是否已经生产
        validatePlanUsed(id);
        // 删除
        itemProduceMapper.deleteById(id);
        // 删除产品工艺路线
        itemProduceRouteService.deleteItemProduceRouteByItemId(id);
    }

    private void validatePlanUsed(Long id) {
        Boolean resp = planService.checkItemUsed(id);
        if (resp) {
            throw exception(ITEM_PRODUCE_USED);
        }
    }

    private void validateCreateOrUpdate(Long id, Long itemId, Long factoryId){
        //同工厂下产品生产信息唯一
        validateItemProduceExists(id, itemId, factoryId);
    }
    private void validateItemProduceExists(Long id, Long itemId, Long factoryId) {

        ItemProduceDO itemProduceDO = itemProduceMapper.selectOne(itemId, factoryId);
        if (ObjectUtil.isEmpty(itemProduceDO)) {
            return;
        }
        if (id == null) {
            throw exception(ITEM_PRODUCE_EXISTS);
        }
        if (!id.equals(itemProduceDO.getId())) {
            throw exception(ITEM_PRODUCE_EXISTS);
        }
    }
    private void validateItemProduceExists(Long id) {
        if (itemProduceMapper.selectById(id) == null) {
            throw exception(ITEM_PRODUCE_NOT_EXISTS);
        }
    }

    @Override
    public ItemProduceDO getItemProduce(Long id) {
        return itemProduceMapper.selectById(id);
    }

    @Override
    public ItemProduceRespVO getItemProduceInfo(Long id) {
        //组装产品工艺路线信息
        List<ItemProduceRouteRespVO> itemProduceRouteRespVOS = itemProduceRouteService.getListByItemProduceId(id);
        ItemProduceDO itemProduceDO = itemProduceMapper.selectById(id);
        ItemProduceRespVO resp = ItemProduceConvert.INSTANCE.convert(itemProduceDO);
        resp.setRouteList(itemProduceRouteRespVOS);
        return resp;
    }

    @Override
    public List<ItemProduceDO> getItemProduceList(Collection<Long> ids) {
        return itemProduceMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ItemProduceDO> getItemProducePage(ItemProducePageReqVO pageReqVO) {
        return itemProduceMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ItemProduceDO> getItemProduceList(ItemProduceExportReqVO exportReqVO) {
        return itemProduceMapper.selectList(exportReqVO);
    }

    @Override
    public ItemProduceDO getItemProduce(Long factoryId, Long itemId) {
        return itemProduceMapper.selectByFactoryAndItem(factoryId, itemId);
    }

}
